package io.contract.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import io.contract.common.utils.OkHttpHelper;
import io.contract.dto.BinanceKlineEvent;
import io.contract.service.BinanceService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BinanceServiceImpl implements BinanceService {

    @SneakyThrows
    @Override
    public String getPerpeExchangeInfo(String baseUrl, String exchange) {
        Response response = OkHttpHelper.get(baseUrl + "/fapi/v1/exchangeInfo");
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        JSONArray array = jsonObject.getJSONArray("symbols");
        for (int i = 0; i < array.size(); i++) {
            JSONObject symbol = array.getJSONObject(i);
            String symbolName = symbol.getString("symbol");
            if (symbolName.equals(exchange)) {
                return symbol.toJSONString();
            }
        }
        return null;
    }

    @Override
    public BinanceKlineEvent getKLine(String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, BinanceKlineEvent.class);
    }

}
